package com.starblink.android.common.view.post;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.starblink.android.basic.sensorsdata.exposure.config.ExposeConfig;
import com.starblink.android.basic.sensorsdata.extension.TrackExtKt;
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef;
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef;
import com.starblink.android.basic.util.ImageUtils;
import com.starblink.android.common.R;
import com.starblink.rocketreserver.FetchResourceConfigQuery;
import com.youth.banner.adapter.BannerAdapter;

import java.util.ArrayList;
import java.util.Map;

import kotlin.Unit;
import kotlin.jvm.functions.Function3;

/**
 * 自定义布局，下面是常见的图片样式，更多实现可以看demo，可以自己随意发挥
 */
public class BannerImageAdapter extends BannerAdapter<FetchResourceConfigQuery.FetchResourceConfig, BannerImageAdapter.BannerViewHolder> {

    Activity context;
    Function3<String, AppCompatImageView, Integer, Unit> listener;

    public BannerImageAdapter(ArrayList<FetchResourceConfigQuery.FetchResourceConfig> mDatas, Activity context, Function3<String, AppCompatImageView, Integer, Unit> listener) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.listener = listener;
        this.context = context;
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_image_banner_post, parent, false);
        return new BannerViewHolder(view);
    }

    @Override
    public void onBindView(BannerViewHolder holder, FetchResourceConfigQuery.FetchResourceConfig data, int position, int size) {
//        int itemWidth = ScreenUtils.getScreenWidth(context) - AutoSizeUtils.dp2px(16f);

        ImageUtils.loadImage(context, data.getResourceF().getMainPic(), holder.img);
        String jumpUrl = data.getResourceF().getJumpUrl();

//        holder.img.setOnClickListener(v -> {
//            listener.invoke(jumpUrl, holder.img);
//        });
        Map<String, String> map = Map.of("index", position + "");
        TrackExtKt.trackData(holder.img, SpmPageDef.RecommendPage, SpmElementDef.RecommendBannerCardV60, map, ExposeConfig.showRatio,
                ExposeConfig.exposeTime, false, null,
                TrackExtKt.businessSuffixId(data.getResourceF()), null);
        listener.invoke(jumpUrl, holder.img, position);
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        AppCompatImageView img;

        public BannerViewHolder(@NonNull View view) {
            super(view);
            this.img = view.findViewById(R.id.iv_post);
        }
    }
}

